<template>
  <transition name="slide">
    <music-list :title="title" :bg-img="bgImg" :songs="songs[0]"></music-list>
  </transition>
</template>

<script type="text/ecmascript-6">
  import MusicList from 'components/music-list/music-list'
  import {mapGetters} from 'vuex'
  import {getSingerDetail} from '@/api/singers'
  import {createSong} from 'common/js/song'
  import {getSongVkey} from 'api/singers'

  export default {
    data () {
      return {
        // 获取被点击歌手的歌曲信息
        songs: []
      }
    },
    computed: {
      // 歌曲详情页标题
      title () {
        return this.singer.name
      },
      // 歌曲详情页背景
      bgImg () {
        return this.singer.avatar
      },
      ...mapGetters([
        'singer'
      ])
    },
    created () {
      this._getDetail()
    },
    methods: {
      _getDetail () {
        // 在歌手详情页刷新时是无歌手id的
        // 所以为了防止一片空白，回退到歌手列表页
        if (!this.singer.id) {
          this.$router.push('/singer')
          return
        }
        // 规范化处理获取到的歌曲信息
        getSingerDetail(this.singer.id).then((res) => {
          let list = res.data.data.list
          this.songs.push(this._normalizeSongs(list))
        })
      },
      // 对当前歌手的歌曲信息进行处理规范
      _normalizeSongs (list) {
        let ret = []
        list.forEach((item) => {
          let {musicData} = item
          getSongVkey(musicData.songmid).then((res) => {
            const vkey = res.data.items[0].vkey
            if (musicData.songid && musicData.albummid) {
              ret.push(createSong(musicData, vkey))
            }
          })
        })
        return ret
      }
    },
    components: {
      MusicList
    }
  }
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/variable.styl"
  .slide-enter-active, .slide-leave-active
    transition: all 0.3s

  .slide-enter, .slide-leave-to
    transform: translate3d(100%, 0, 0)
</style>
